#include <gccore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wiiuse/wpad.h>
#include "loaddol.h"
#include "processor.h"
#include "dol.h"
#include "elf.h"
#include "../controller.h"
#include "../fat.h"
#include "../gecko.h"
#include "../smb.h"
#include "../tools.h"
#include "../video.h"
#include "../wadmanager.h"

extern void __exception_closeall();

int loadelfdol(char *path) {
    void* myBuffer;
    void (*ep)();
    path = trim(path);
    
    FILE* inputFile;
    inputFile = fopen(path, "rb");

    if (inputFile == NULL) {
        printf("Dol/Elf file does not exist in the path specified in the config file: ");
        printf(path);
        printf("Press A to reboot Wii...\n");
        flushBuffer();

        u32 pressed;
        ScanPads(&pressed);
        if (pressed & WPAD_BUTTON_A) {
            SYS_ResetSystem(SYS_RESTART, 0, 0);
        }
        return 0;
    }
	
	printf("Starting homebrew...\n");

    int pos = ftell(inputFile);
    fseek(inputFile, 0, SEEK_END);
    int size = ftell(inputFile);
    fseek(inputFile, pos, SEEK_SET); //return to previous position

    myBuffer = malloc(size);
    fread(myBuffer, 1, size, inputFile);
    
    //Check if valid elf file:
    s32 res;
    res = valid_elf_image(myBuffer);
    //printf("checked elf valid\n");
    if (res == 1) {
        //Elf ok! -> Load entry point of elf file:
        ep = (void(*)()) load_elf_image(myBuffer);
    } else {
        ep = (void(*)()) load_dol_image(myBuffer);
    }

    gprintf("Entry point: 0x%X\n", (unsigned int) ep);

    fclose(inputFile);
    
    if (!strcmp(fdev->mount, "smb")) {
        CloseSMB();
    } else {
        Fat_Unmount(fdev);
    }
    
    // code from geckoloader
    u32 level;
    __IOS_ShutdownSubsystems();
    gprintf("IOS_ShutdownSubsystems() done\n");
    _CPU_ISR_Disable (level);
    //gprintf("_CPU_ISR_Disable() done\n");
    __exception_closeall();
    gprintf("__exception_closeall() done. Jumping to ep now...\n");
    ep();
    _CPU_ISR_Restore (level);
    return 0;
}
